package com.itouchtv.yang.core.dataBase;

import com.itouchtv.yang.core.tools.Log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
@Deprecated
public class Connect {
    private static Connection connection;
    private static Statement statement;
    private Connect(){}

    public static Statement getStatement(){
        try {
            if (statement==null){
                setConnection();
            }else if(statement.isClosed()){
                statement = connection.createStatement();
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }

        return statement;
    }

    private static void setConnection(){
        String url = "jdbc:mysql://192.168.31.49:3306/touchtv_test";
        String user = "touchtv";
        String password = "op@touchtv";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, user, password);
            statement = connection.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            Log.logger.error("没有找到JDBC",e.fillInStackTrace());
        } catch (SQLException e) {
            Log.logger.error("数据库连接失败",e.fillInStackTrace());
            e.printStackTrace();
        }
    }

    public static void closeConnect(){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                Log.logger.error("statement关闭失败",e.fillInStackTrace());
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                Log.logger.error("connection关闭失败",e.fillInStackTrace());
                e.printStackTrace();
            }
        }
    }
}
